package cn.tedu.ivos.application.service.impl;

import cn.tedu.ivos.application.mapper.ApplicationMapper;
import cn.tedu.ivos.application.pojo.dto.ApplicationQuery;
import cn.tedu.ivos.application.pojo.dto.ApplicationSaveParam;
import cn.tedu.ivos.application.pojo.entity.Application;
import cn.tedu.ivos.application.pojo.vo.ApplicationVO;
import cn.tedu.ivos.application.service.ApplicationService;
import cn.tedu.ivos.audit.mapper.AuditMapper;
import cn.tedu.ivos.audit.pojo.vo.AuditVO;
import cn.tedu.ivos.audit.service.AuditService;
import cn.tedu.ivos.base.enums.ApplicationStatusEnum;
import cn.tedu.ivos.user.mapper.UserMapper;
import cn.tedu.ivos.user.pojo.vo.UserVO;
import cn.tedu.ivos.user.service.UserService;
import cn.tedu.ivos.vehicle.mapper.VehicleMapper;
import cn.tedu.ivos.vehicle.pojo.entity.Vehicle;
import com.fasterxml.jackson.databind.util.BeanUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.StringJoiner;

@Slf4j
@Service
@Transactional
public class ApplicationServiceImpl implements ApplicationService {
    @Autowired
    ApplicationMapper applicationMapper;
    @Autowired
    AuditService auditService;
    @Autowired
    UserMapper userMapper;
    @Autowired
    AuditMapper auditMapper;
    @Autowired
    VehicleMapper vehicleMapper;

    @Override
    public void save(ApplicationSaveParam applicationSaveParam) {
        log.debug("开始执行【保存用车申请单】的业务层方法,applicationSaveParam={}", applicationSaveParam);
        Application application = new Application();
        BeanUtils.copyProperties(applicationSaveParam,application);
        application.setStatus(ApplicationStatusEnum.PENDING.getCode());
        application.setCreateTime(new Date());
        // 遇到的问题,新增申请单对应的审批单时,审批表中没有得到申请单的id
        // 原因:id是一个很特殊的属性,无法通过其他字段进行select查询
        // 解决方案是: 给此insert()对应的SQL上加配置 userGeneratedKeys="true" keyProperty="id"
        // 效果: JDBC会为当前的application对象自动生产自增主键,并把生成的主键值自动赋值给当前的application对象的id属性
        applicationMapper.insert(application);

        auditService.insertAudit(application);

    }

    @Override
    public List<ApplicationVO> selectAll(ApplicationQuery applicationQuery) {
        log.debug("开始执行【查询所有申请单】的业务层方法,applicationQuery={}", applicationQuery);
        List<ApplicationVO> list = applicationMapper.selectAll(applicationQuery);
        // 遍历上方的申请单集合,为每一个申请单VO对象补充审批人数据
        for (int i = 0; i < list.size(); i++){
            ApplicationVO applicationVO = list.get(i);
            // 处理申请单VO,给当前的申请单VO对象补充审批人信息
            assignAuditUserList(applicationVO);
        }
        return list;
    }

    @Override
    public void cancel(Long id) {
        log.debug("开始执行【撤销申请单】的业务层方法,id={}", id);
        Application application = new Application();
        application.setId(id);
        application.setStatus(ApplicationStatusEnum.CANCEL.getCode());
        application.setUpdateTime(new Date());
        // 修改申请单状态
        applicationMapper.cancelStatus(application);
        // 删除申请单对应的审批单
        auditMapper.deleteByApplicationId(id);

    }

    @Override
    public void distribute(Long id, Long vehicleId) {
        log.debug("开始执行【分配车辆】的业务层方法,id={},vehicleId={}", id, vehicleId);
        // 修改申请单状态
        Application application = new Application();
        application.setId(id);
        application.setVehicleId(vehicleId);
        application.setUpdateTime(new Date());
        application.setStatus(ApplicationStatusEnum.ALLOCATION.getCode());
        applicationMapper.cancelStatus(application);
        // 修改车辆状态
        Vehicle vehicle = new Vehicle();
        vehicle.setId(vehicleId);
        vehicle.setStatus("2");
        vehicle.setUpdateTime(new Date());
        vehicleMapper.updateVehicle(vehicle);

    }

    @Override
    public void back(Long id, Long vehicleId) {
        log.debug("开始执行【归还车辆】的业务层方法,id={},vehicleId={}", id, vehicleId);
        // 修改申请单状态
        //方案一: 保留申请单的车辆id
//        Application application = new Application();
//        application.setId(id);
//        application.setVehicleId(vehicleId);
//        application.setUpdateTime(new Date());
//        application.setStatus(ApplicationStatusEnum.END.getCode());
//        applicationMapper.cancelStatus(application);
        //方案二: 把申请单中的车辆id 改成 null
        Application application = new Application();
        application.setId(id);
        application.setVehicleId(null);
        application.setUpdateTime(new Date());
        application.setStatus(ApplicationStatusEnum.END.getCode());
        applicationMapper.back(application);
        // 修改车辆状态
        Vehicle vehicle = new Vehicle();
        vehicle.setId(vehicleId);
        vehicle.setStatus("1");
        vehicle.setUpdateTime(new Date());
        vehicleMapper.updateVehicle(vehicle);
    }

    private void assignAuditUserList(ApplicationVO applicationVO) {
        // 准备空集合,分别用来保存审批人id和审批人姓名
        List<String> auditUsernameList = new ArrayList<>();
        List<Long> auditUserIdList = new ArrayList<>();
        // 根据传入的申请单对象,查出对应的审批单
        List<AuditVO> auditVOList = auditService.selectByApplicationId(applicationVO.getId());
        // 遍历依据申请单id查出来的审批单集合
        for (int i = 0; i < auditVOList.size(); i++){
            AuditVO auditVO = auditVOList.get(i);
            // 取出当前审批单中的审批人id,并存入上方的空集合中
            Long id = auditVO.getAuditUserId();
            auditUserIdList.add(id);
            // 5.根据刚刚取出的审批人id,查出对应的审批人姓名,并存入上方的空集合中
            UserVO userVO = userMapper.selectByParentId(id);
            auditUsernameList.add(userVO.getUsername());
        }
        // 6.准备一个拼接工具,帮我们将所有审批人姓名连接成一个字符串
        StringJoiner stringJoiner = new StringJoiner(",");
        for( String username : auditUsernameList){
            stringJoiner.add(username);
        }
        //将拼接好的审批人姓名字符串赋值给申请单VO的AuditUsernameList属性
        applicationVO.setAuditUsernameList(stringJoiner.toString());
        //将准备好的审批人id集合赋值给申请单VO的AuditUserIdList属性
        applicationVO.setAuditUserIdList(auditUserIdList);
    }
}
